package com.tools.web.interceptor.access.spel;

import com.tools.common.exception.service.ValidException;
import com.tools.common.object.C;
import com.tools.common.object.Note;
import com.tools.common.object.ObjKit;
import com.tools.web.GlobalKit;
import com.tools.web.WebKit;
import com.tools.web.work.ResultCode;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 通用参数校验处理器
 * */
@Note("通用参数校验处理器")
abstract class GenericParamValidHandler {


    GenericParamValidHandler() {}

    /* *****************************************************************************************
     *
     *      功能方法
     *
     * *****************************************************************************************
     * */

    public String getIP(HttpServletRequest request) {
        return GlobalKit.getClientIP(request);
    }

    public String getPubIP(HttpServletRequest request) {
        return GlobalKit.getPublicClientIP(request);
    }

    public String getStrId(HttpServletRequest request) {
        return GlobalKit.getStringUserId(request);
    }

    public String getStrId() {
        return getStrId(WebKit.getHttpServletRequest());
    }

    public long getLongId(HttpServletRequest request) {
        return GlobalKit.getLongUserId(request);
    }

    public long getLongId() {
        return getLongId(WebKit.getHttpServletRequest());
    }

    public int getIntId(HttpServletRequest request) {
        return GlobalKit.getIntUserId(request);
    }

    public int getIntId() {
        return getIntId(WebKit.getHttpServletRequest());
    }

    public Object getUser(HttpServletRequest request) {
        return GlobalKit.getAuthUser(request).getUser();
    }

    public Object getUser() {
        return getUser(WebKit.getHttpServletRequest());
    }

    public <T> T getUser(HttpServletRequest request, Class<T> beanType) {
        return GlobalKit.getAuthUser(request).getUser(beanType);
    }

    public <T> T getUser(Class<T> beanType) {
        return getUser(WebKit.getHttpServletRequest(), beanType);
    }

    public <T> T getUser(HttpServletRequest request, C<T> beanType) {
        return GlobalKit.getAuthUser(request).getUser(beanType);
    }

    public <T> T getUser(C<T> beanType) {
        return getUser(WebKit.getHttpServletRequest(), beanType);
    }

    public <T> T get(HttpServletRequest request, String name, C<T> valueType) {
        try {
            String parameter = request.getParameter(name);
            return (T) ObjKit.stringToRealValue(parameter, valueType);
        } catch (Exception e) {
            throw new ValidException(ResultCode.PARAM_ERROR.getResult());
        }
    }

    public String getStr(HttpServletRequest request, String name) {
        return request.getParameter(name);
    }

    public Integer getInt(HttpServletRequest request, String name) {
        return this.get(request, name, C.build(Integer.class));
    }

    public Long getLong(HttpServletRequest request, String name) {
        return this.get(request, name, C.build(Long.class));
    }

    public Double getDouble(HttpServletRequest request, String name) {
        return this.get(request, name, C.build(Double.class));
    }

    public Boolean getBool(HttpServletRequest request, String name) {
        return this.get(request, name, C.build(Boolean.class));
    }

    /* *****************************************************************************************
     *
     *      等于判断
     *
     * *****************************************************************************************
     * */

    public boolean eq(HttpServletRequest request, String name, int val) {
        Integer i = this.getInt(request, name);
        return i != null && i == val;
    }

    public boolean eq(HttpServletRequest request, String name, long val) {
        Long i = this.getLong(request, name);
        return i != null && i == val;
    }

    public boolean eq(HttpServletRequest request, String name, double val) {
        Double i = this.getDouble(request, name);
        return i != null && i == val;
    }

    public boolean eq(HttpServletRequest request, String name, boolean val) {
        Boolean i = this.getBool(request, name);
        return i != null && i == val;
    }

    public boolean eq(HttpServletRequest request, String name, char val) {
        String param = request.getParameter(name);
        return param != null && !param.isEmpty() && param.charAt(0) == val;
    }

    public boolean eq(HttpServletRequest request, String name, Object value, C<?> valueType) {
        String param = request.getParameter(name);
        if(value == null) return param == null;
        if (Objects.equals(param, value)) return true;
        if(valueType == null) throw new NullPointerException("value 的类型必须明确");
        Object paramValue = ObjKit.stringToRealValue(param, valueType);
        return Objects.equals(paramValue, value);
    }

}
